import axios from "axios";
import cheerio from "cheerio";
import { iArg } from "../iArg";
import { iFetcher } from "../iFetcher";
import { fetchRawData } from "./fetchRawData.js";
class SubmitRecordfetcher implements iFetcher {
  public data: SubmitDetial[] = []
  public async fetch(arg: iArg): Promise<string> {
    const { contestUrl, userName } = arg
    if (contestUrl === undefined || userName === undefined) {
      return ""
    }
    let htmlText = "";
    let url = contestUrl + "/submissions?f.User=" + userName;
    htmlText = await fetchRawData(contestUrl + "/submissions?f.User=" + userName)
    if (htmlText === "") {
      return ""
    }

    let $ = cheerio.load(htmlText)("tbody").find("tr");
    if ($.length === 0) {
      console.error(
        `cant find data in ${contestUrl}/submissions?f.User=${userName}`
      );
      return "";
    }

    for (let i = 0; i < $.length; i++) {
      let detailUrl = $.eq(i).find("td").eq(9).find("a").eq(0).attr("href");
      if (detailUrl == undefined) {
        console.error("cannot parse detialUrl in page " + url);
        continue;
      }
      detailUrl = "https://atcoder.jp" + detailUrl;

      let detial = await FetchSubmitDetial(detailUrl);
      if (detial) {
        this.data.push(detial);
      }
    }
    return JSON.stringify(this.data);
  }
}

class SubmitDetial {
  SubmissionTime: number=0;
  Task: string="";
  User: string="";
  Language: string="";
  Score: string="";
  CodeSize: string="";
  Status: string="";
  ExecTime: string="";
  Memory: string="";
  Code: string="";
}
/**
 *
 * @param contestUrl 比赛url
 * @param userName 查询的用户名
 */
async function fetchUserSubmitRecord(
  contestUrl: string,
  userName: string
): Promise<string> {
  let htmlText = "";
  let url = contestUrl + "/submissions?f.User=" + userName;
  htmlText=await fetchRawData(url)
  if(htmlText===""){
    return ""
  }
  let $ = cheerio.load(htmlText)("tbody").find("tr");
  if ($.length === 0) {
    console.error(
      `cant find data in ${contestUrl}/submissions?f.User=${userName}`
    );
    return "";
  }
  let allDetail: SubmitDetial[] = [];
  for (let i = 0; i < $.length; i++) {
    let detailUrl = $.eq(i).find("td").eq(9).find("a").eq(0).attr("href");
    if (detailUrl == undefined) {
      console.error("cannot parse detialUrl in page " + url);
      continue;
    }
    detailUrl = "https://atcoder.jp" + detailUrl;

    let detial = await FetchSubmitDetial(detailUrl);
    if (detial) {
      allDetail.push(detial);
    }
  }
  return JSON.stringify(allDetail);
}

async function FetchSubmitDetial(url: string): Promise<SubmitDetial | null> {
  let htmlText = "";
  htmlText=await fetchRawData(url)
  if (htmlText===""){
    return null
  }
  try {
    htmlText = (await axios.get(url)).data;
  } catch (err: any) {
    if (err instanceof Error) {
      console.error(err.message);
    }
    return null;
  }
  let $ = cheerio.load(htmlText);
  let code = $("#submission-code").text();
  let td = $(".table").find("td");
  let detail=new SubmitDetial()
  detail.SubmissionTime=Date.parse(td.eq(0).text())
  detail.Task= td.eq(1).text()
  detail.User=td.eq(2).text()
  detail.Language= td.eq(3).text()
  detail.Score= td.eq(4).text()
  detail.CodeSize=td.eq(5).text()
  detail.Status=td.eq(6).text()
  detail.ExecTime=td.eq(7).text()
  detail.Memory= td.eq(8).text()
  detail.Code=code

  return detail
}

export {SubmitRecordfetcher};
